package cn.edu.sgu.www.mhxysy.function.computer.score.impl;

import cn.edu.sgu.www.mhxysy.entity.xingyin.*;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.score.ScoreComputer;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.*;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 星印评分计算器
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class XingyinScoreComputer implements ScoreComputer {

    private final XingyinMapper xingyinMapper;
    private final XingyinCategoryMapper xingyinCategoryMapper;
    private final XingyinAttributeMapper xingyinAttributeMapper;
    private final XingyinPropertyScoreMapper xingshiPropertyScoreMapper;
    private final XingyinTexiaoCategoryMapper xingyinTexiaoCategoryMapper;

    @Autowired
    public XingyinScoreComputer(
            XingyinMapper xingyinMapper,
            XingyinCategoryMapper xingyinCategoryMapper,
            XingyinAttributeMapper xingyinAttributeMapper,
            XingyinPropertyScoreMapper xingshiPropertyScoreMapper,
            XingyinTexiaoCategoryMapper xingyinTexiaoCategoryMapper) {
        this.xingyinMapper = xingyinMapper;
        this.xingyinCategoryMapper = xingyinCategoryMapper;
        this.xingyinAttributeMapper = xingyinAttributeMapper;
        this.xingshiPropertyScoreMapper = xingshiPropertyScoreMapper;
        this.xingyinTexiaoCategoryMapper = xingyinTexiaoCategoryMapper;
    }

    /**
     * 计算星印评分
     * @param xingyinId 星印ID
     * @return Integer 星印评分
     */
    @Override
    public Integer apply(String xingyinId) {
        Xingyin xingyin = xingyinMapper.selectById(xingyinId);

        if (xingyin == null) {
            throw new GlobalException(ResponseCode.CONFLICT, "操作失败，星印不存在！");
        }

        /*
         * 星印评分 = 星印基础评分 + 星印特效评分
         * 1、星印基础评分固定为50
         * 2、星印特效评分可以从星印特效表查询得到
         */
        AtomicInteger score = new AtomicInteger();

        /*
         * 1、星印基础评分
         */
        score.addAndGet(50);

        /*
         * 2、星印特效评分
         */
        Integer texiaoId = xingyin.getTexiaoId();

        if (texiaoId != null) {
            // 查询星印类型信息
            XingyinCategory xingyinCategory = xingyinCategoryMapper.selectById(xingyin.getCategoryId());
            // 查询星印特效类型信息
            XingyinTexiaoCategory xingyinTexiaoCategory = xingyinTexiaoCategoryMapper.selectById(texiaoId);

            BigDecimal bigDecimal = new BigDecimal(
                    xingyinTexiaoCategory.getScore()
            ).multiply(
                    // 星印特效等级系数 = 1.0 + ((星印等级 - 65) / 20) * 0.5
                    BigDecimal.valueOf(1.0).add(
                            BigDecimal.valueOf(
                                    (xingyinCategory.getGrade() - 65) / 20
                            ).multiply(BigDecimal.valueOf(0.5))
                    )
            );

            score.addAndGet(bigDecimal.intValue());
        }

        /*
         * 3、星印属性评分
         */
        // 查询星印属性
        QueryWrapper<XingyinAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("xingyin_id", xingyinId);

        List<XingyinAttribute> list = xingyinAttributeMapper.selectList(queryWrapper);

        if (CollectionUtils.isNotEmpty(list)) {
            for (XingyinAttribute xingyinAttribute : list) {
                Map<String, Object> columnMap = new HashMap<>();

                columnMap.put("property_id", xingyinAttribute.getPropertyId());

                List<XingyinPropertyScore> scores = xingshiPropertyScoreMapper.selectByMap(columnMap);

                XingyinPropertyScore propertyScore = null;

                if (scores.size() == 1) {
                    propertyScore = scores.get(0);
                } else if (scores.size() == 2) {
                    // 基础属性和鉴定属性评分计算不一致
                    Integer type = xingyinAttribute.getType();

                    if (type != null) {
                        columnMap.put("type", xingyinAttribute.getType());

                        propertyScore = xingshiPropertyScoreMapper.selectByMap(columnMap).get(0);
                    }
                }

                if (propertyScore != null) {
                    score.addAndGet(
                            BigDecimal.valueOf(
                                    xingyinAttribute.getPropertyValue()
                            ).multiply( // 乘以乘积系数
                                    BigDecimal.valueOf(propertyScore.getMultiply())
                            ).divide( // 除以除商系数
                                    BigDecimal.valueOf(propertyScore.getDivide()), BigDecimal.ROUND_DOWN
                            ).intValue()
                    );
                }
            }
        }

        return score.get();
    }

}